home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 32 / advsys.zip / ADVIIO.C < prev    next >
Text File  |  1987-06-28  |  8KB  |  323 lines

  1. /* adviio.c - system dependant code for AdvInt */
  2. /*
  3.     Copyright (c) 1986, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9.  
  10. /* status values */
  11. #define ST_OK    1
  12. #define ST_ERR    0
  13.  
  14. /* external routines */
  15. extern long lseek();
  16.  
  17. /* advinit - initialize */
  18. /*
  19.     This function parses the argument last and returns the
  20.     data and log file names as well as the number of rows
  21.     and columns on the user's screen.
  22. */
  23. advinit(banner,argc,argv,dname,lname,prows,pcols)
  24.   char *banner;    /* program banner text */
  25.   int argc;    /* argc passed to main() */
  26.   char *argv[];    /* argv passed to main() */
  27.   char *dname;    /* data file name (output) */
  28.   char *lname;    /* log file name (output) */
  29.   int *prows;    /* number of screen rows (output) */
  30.   int *pcols;    /* number of screen columns (output) */
  31. {
  32.     int i;
  33.     
  34.     /* print the banner text */
  35.     printf("%s\n",banner);
  36.     
  37.     /* initialize and setup the defaults */
  38.     dname[0] = '\0';
  39.     lname[0] = '\0';
  40.     *prows = 24;
  41.     *pcols = 80;
  42.  
  43.     /* parse the command line */
  44.     for (i = 1; i < argc; ++i)
  45.     if (argv[i][0] == '-')
  46.         switch (argv[i][1]) {
  47.         case 'r':    /* set the number of screen rows */
  48.         case 'R':
  49.             *prows = atoi(&argv[i][2]);
  50.             break;
  51.         case 'c':    /* set the number of screen columns */
  52.         case 'C':
  53.             *pcols = atoi(&argv[i][2]);
  54.             break;
  55.         case 'l':    /* set the log file name */
  56.         case 'L':
  57.             lname = &argv[i][2];
  58.                 break;
  59.         }
  60.     else
  61.         strcpy(dname,argv[i]);
  62.  
  63.     /* make sure we got a data file name */
  64.     if (dname[0] == '\0') {
  65.     fprintf(stderr,
  66.         "usage: advint [-r<rows>] [-c<columns>] [-l<log-file>] <file>\n");
  67.     exit(1);
  68.     }
  69.  
  70.     /* add the default extension */
  71.     strcat(dname,".dat");
  72. }
  73.  
  74. /* advgetc - get a character from the terminal */
  75. /*
  76.     This routine reads a single character from the terminal.  The
  77.     character should be echoed and the RETURN or ENTER key should
  78.     cause a '\n' character to be returned.
  79. */
  80. int advgetc()
  81. {
  82. /* This could be just:
  83.     return (getchar());
  84. */
  85.     int ch;
  86.     if ((ch = bdos(1) & 0xFF) == '\r') {
  87.     bdos(6,'\n');
  88.     ch = '\n';
  89.     }
  90.     return (ch);
  91. }
  92.  
  93. /* advputc - output a character to the terminal (newline is a '\n') */
  94. /*
  95.     This routine writes a single character to the terminal.  The
  96.     character '\n' should result in the cursor being positioned
  97.     at the first column of the next line.
  98. */
  99. advputc(ch)
  100.   int ch;    /* character to output */
  101. {
  102. /* This could be just:
  103.     putchar(ch);
  104. */
  105.     if (ch == '\n')
  106.     bdos(6,'\r');
  107.     bdos(6,ch);
  108. }
  109.  
  110. /* advwaitc - wait for a key to be hit */
  111. /*
  112.     This function waits for a key to be hit.  If you can do this
  113.     without echoing the character, you can wait for any character.
  114.     If your runtime system does echo characters, your best bet
  115.     would be to wait for the user to type RETURN or ENTER.
  116. */
  117. advwaitc()
  118. {
  119. /* This could be just:
  120.     while (getchar() != '\n');
  121. */
  122.     bdos(7);
  123. }
  124.  
  125. /* advlogc - output a character to the log file */
  126. /*
  127.     This routine writes a single character to an ASCII file.  It
  128.     was added as a hook for the Aztec C runtime routines that
  129.     require aputc() instead of putc() for ASCII files.  You should
  130.     probably use putc() here.  Note that the file pointer passed
  131.     as a parameter to this routine is the value returned from
  132.     the fopen() routine called with the file mode of "w".
  133. */
  134. advlogc(ch,fp)
  135.   int ch;    /* character to output */
  136.   FILE *fp;    /* output file pointer */
  137. {    
  138. /* This could be just:
  139.     putc(ch,fp);
  140. */
  141.     aputc(ch,fp); /* Aztec C uses aputc for ASCII output */
  142. }
  143.  
  144. /* advcreate - create a binary data file */
  145. /*
  146.     This function *must* create the file in binary mode so that
  147.     subsequent writes will also be in binary mode.  No character
  148.     translations should be done during writes.  You probably
  149.     won't need to modify this function unless your runtime
  150.     library defaults to ASCII mode for files created with creat().
  151. */
  152. int advcreate(name,pfd)
  153.   char *name;    /* file name */
  154.   int *pfd;    /* file descriptor (output) */
  155. {
  156.     /* create the file in ***BINARY*** mode */
  157.     if ((*pfd = creat(name,0666)) < 0)
  158.     return (ST_ERR);
  159.  
  160.     /* return successfully */
  161.     return (ST_OK);
  162. }
  163.  
  164. /* advopen - open a binary data file */
  165. /*
  166.     This function *must* open the file in binary mode so that
  167.     subsequent reads will also be in binary mode.  No character
  168.     translations should be done during reads.  You probably
  169.     won't need to modify this function unless your runtime
  170.     library defaults to ASCII mode for files opened with open().
  171. */
  172. int advopen(name,pfd)
  173.   char *name;    /* file name */
  174.   int *pfd;    /* file descriptor (output) */
  175. {
  176.     /* open the file in ***BINARY*** mode */
  177.     if ((*pfd = open(name,O_RDONLY)) < 0)
  178.     return (ST_ERR);
  179.     
  180.     /* return successfully */
  181.     return (ST_OK);
  182. }
  183.  
  184. /* advclose - close a binary data file */
  185. /*
  186.     You probably won't need to modify this routine.  It was
  187.     added as a hook to allow the use of non-UNIX style file
  188.     I/O routines.
  189. */
  190. int advclose(fd)
  191.   int fd;    /* file descriptor */
  192. {
  193.     /* close the binary file */
  194.     if (close(fd) != 0)
  195.     return (ST_ERR);
  196.  
  197.     /* return successfully */
  198.     return (ST_OK);
  199. }
  200.  
  201. /* advseek - seek to a position in a binary data file */
  202. /*
  203.     You probably won't need to modify this routine.  It was
  204.     added as a hook to allow the use of non-UNIX style file
  205.     I/O routines.
  206. */
  207. int advseek(fd,pos)
  208.   int fd;    /* file descriptor */
  209.   long pos;    /* new file position */
  210. {
  211.     /* seek to the position */
  212.     if (lseek(fd,pos,0) != pos)
  213.     return (ST_ERR);
  214.  
  215.     /* return successfully */
  216.     return (ST_OK);
  217. }
  218.  
  219. /* advread - read from a binary data file */
  220. /*
  221.     This function must read data in binary mode.  No character
  222.     translations should be done here.
  223. */
  224. int advread(fd,buf,len)
  225.   int fd;    /* file descriptor */
  226.   char *buf;    /* buffer address */
  227.   int len;    /* number of bytes to read */
  228. {
  229.     /* read the data */
  230.     if (read(fd,buf,len) != len)
  231.     return (ST_ERR);
  232.  
  233.     /* return successfully */
  234.     return (ST_OK);
  235. }
  236.  
  237. /* advwrite - write to a binary data file */
  238. /*
  239.     This function must write data in binary mode.  No character
  240.     translations should be done here.
  241. */
  242. int advwrite(fd,buf,len)
  243.   int fd;    /* file descriptor */
  244.   char *buf;    /* buffer address */
  245.   int len;    /* number of bytes to write */
  246. {
  247.     /* write the data */
  248.     if (write(fd,buf,len) != len)
  249.     return (ST_ERR);
  250.  
  251.     /* return successfully */
  252.     return (ST_OK);
  253. }
  254.  
  255. /* advsfile - get file name to restore */
  256. /*
  257.     You can probably leave this function as-is.  It was added
  258.     to the machine/compiler specific module to provide a hook
  259.     for using file requesters on machines like the Macintosh
  260.     and the Atari-ST.
  261. */
  262. int advsfile(name)
  263.   char *name;    /* file name buffer (output) */
  264. {  
  265.     /* get the filename */
  266.     trm_str("File to save (without the \".sav\")? ");
  267.     trm_get(name);
  268.  
  269.     /* add the default extension */
  270.     strcat(name,".sav");
  271.     
  272.     /* return status (ST_OK if the user typed something */
  273.     return (*name != '.' ? ST_OK : ST_ERR);
  274. }
  275.  
  276. /* advrfile - get file name to restore */
  277. /*
  278.     You can probably leave this function as-is.  It was added
  279.     to the machine/compiler specific module to provide a hook
  280.     for using file requesters on machines like the Macintosh
  281.     and the Atari-ST.
  282. */
  283. int advrfile(name)
  284.   char *name;    /* file name buffer (output) */
  285. {  
  286.     /* get the filename */
  287.     trm_str("File to restore (without the \".sav\")? ");
  288.     trm_get(name);
  289.  
  290.     /* add the default extension */
  291.     strcat(name,".sav");
  292.     
  293.     /* return status (ST_OK if the user typed something */
  294.     return (*name != '.' ? ST_OK : ST_ERR);
  295. }
  296.  
  297. /* advfatal - print a fatal error message and exit */
  298. /*
  299.     You can probably leave this function as-is.  It was added
  300.     to the machine/compiler dependant module to provide a hook
  301.     for using alert boxes on machines like the Macintosh and
  302.     the Atari-ST.
  303. */
  304. advfatal(msg)
  305.   char *msg;    /* error message */
  306. {
  307.     trm_str(msg);
  308.     trm_chr('\n');
  309.     exit(1);
  310. }
  311.  
  312. /* advpause - pause at the end of the adventure before returning to the OS */
  313. /*
  314.     This routine is provided as a hook for machines that clear the
  315.     screen before returning to the OS.  It can be used to prevent
  316.     the text that is displayed at the end of an adventure from
  317.     being erased before the player has a chance to see it.
  318. */
  319. advpause()
  320. {
  321. }
  322.  
  323.